package com.Maybe.book.service;

import com.Maybe.book.dao.BookDao;
import com.Maybe.book.enums.BookStatusEnum;
import com.Maybe.book.mapper.BookMapper;
import com.Maybe.book.model.BookInfo;
import com.Maybe.book.model.PageRequest;
import com.Maybe.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class Bookservice {

    @Autowired
    private BookMapper bookMapper;
//    public List<BookInfo> getList(){
//        List<BookInfo> bookInfos=bookDao.mockData();
//        for(BookInfo bookInfo:bookInfos){
//            if(bookInfo.getStatus()==1){
//                bookInfo.setStatusCN("可借阅");
//            }else{
//                bookInfo.setStatusCN("不可借阅");
//            }
//        }
//        return bookInfos;
//    }

    public void addBook(BookInfo bookInfo) {
        bookMapper.addBook(bookInfo);
    }

    public PageResult<BookInfo> getlistBypage(PageRequest pageRequest) {
        Integer count= bookMapper.count();
        List<BookInfo> books=bookMapper.selectBooksByPage(pageRequest);
        for(BookInfo book:books){
           book.setStatusCN(BookStatusEnum.getStatusBycode(book.getStatus()).getDesc());
        }
        return new PageResult<>(count,books,pageRequest);

    }

    public BookInfo queryBookById(Integer id) {
        return bookMapper.queryBookById(id);
    }

    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);

    }

    public void batchDelete(List<Integer> ids) {
        bookMapper.batchDeleteBook(ids);
    }
}
